
#ifndef __LIBERTY_ENGINE_GLEXCEPTION_H__
#define __LIBERTY_ENGINE_GLEXCEPTION_H__

#include <Liberty3D/Liberty3D.h>
#include <Liberty3D/OpenGL.h>

#include <stdexcept>

namespace Liberty { namespace Engine {

    class GLException : public std::runtime_error {
    protected:
        GLenum _err;

    public:
        GLException(GLenum err, const char* error);

    public:
        static void raiseEnterFunction(GLenum err, const char* function, const char* file, unsigned int line);
        static void raiseCallFunction (GLenum err, const char* glFunction, const char* function, const char* file, unsigned int line);
    };

}}

#define LY_CHECKED_GL_CALLER() \
    GLenum __gl_err = glGetError(); \
    if (__gl_err != GL_NO_ERROR) { \
        Liberty::Engine::GLException::raiseEnterFunction(__gl_err, __FUNCTION__, __FILE__, __LINE__); \
    }

#define LY_CHECKED_GL_CALL(glFunction, parameters) \
    glFunction parameters; \
    __gl_err = glGetError(); \
    if (__gl_err != GL_NO_ERROR) { \
        Liberty::Engine::GLException::raiseCallFunction(__gl_err, #glFunction, __FUNCTION__, __FILE__, __LINE__); \
    }

#endif
